
const homeController = function(HttpService) {
  'ngInject';

  const vm = this;

  vm.stats = {
    total_tickets: 0,
    pending_tickets: 0,
    in_progress_tickets: 0,
    resolved_tickets: 0,
    pending_todos: 0,
    completed_todos: 0
  };

  vm.recentTickets = [];
  vm.isLoading = true;

  // 初始化加载数据
  vm.$onInit = function() {
    loadDashboardData();
  };

  function loadDashboardData() {
    vm.isLoading = true;
    
    HttpService.stats.dashboard()
      .then(response => {
        if (response.success) {
          vm.stats = response.data.summary;
          vm.recentTickets = response.data.recent_tickets.map(ticket => ({
            ...ticket,
            createTime: new Date(ticket.created_at).toLocaleDateString()
          }));
        }
      })
      .catch(error => {
        console.error('加载仪表盘数据失败:', error);
      })
      .finally(() => {
        vm.isLoading = false;
      });
  }

  vm.getStatusBadgeClass = function(status) {
    const statusClasses = {
      'pending': 'badge-warning',
      'in_progress': 'badge-info',
      'resolved': 'badge-success',
      'closed': 'badge-secondary'
    };
    return statusClasses[status] || 'badge-secondary';
  };

  vm.getStatusText = function(status) {
    const statusText = {
      'pending': '待处理',
      'in_progress': '处理中',
      'resolved': '已解决',
      'closed': '已关闭'
    };
    return statusText[status] || status;
  };
};

const homeComponent = {
  templateUrl: 'src/app/pages/home/home.template.html',
  controller: homeController
};

function initHomeController() {
  const angular = window.angular;
  
  if (!angular) {
    console.error('Angular 未找到，无法初始化首页控制器模块');
    return;
  }

  angular.module('app.pages.home', [])
    .component('homePage', homeComponent);
}

// Listen for vendor libs loaded event
window.addEventListener('vendorLibsLoaded', () => {
  initHomeController();
});

// If Angular already loaded, init directly  
if (window.angular) {
  initHomeController();
}